package lib.quasar.widget.chart.draw;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextUtils;

import java.util.List;

import lib.quasar.util.DimenUtil;
import lib.quasar.widget.chart.ChartView;
import lib.quasar.widget.chart.entry.Entry;
import lib.quasar.widget.chart.paint.StockPaint;

/**
 * description: 出入量
 * created by kalu on 2018/4/25 9:19
 */
public final class DrawUrinevolume implements IDraw {

    private float minValue = 0f; // Y轴最小值
    private float maxValue = 10000f; // Y轴最大值

    private float left, right, top, bottom;
    private float marginLeft, marginRight, marginTop, marginBottom;
    private float width, height, centerX, centerY;
    private float paddingLeft, paddingTop, paddingRight, paddingBottom;

    private final Rect mTextRect = new Rect();
    private final float[] mDashLine = new float[4];

    @Override
    public void onDrawInit(float left, float top, float right, float bottom,
                           float marginLeft, float marginTop, float marginRight, float marginBottom,
                           float paddingLeft, float paddingTop, float paddingRight, float paddingBottom,
                           float width, float height, float centerX, float centerY, int model) {

        if (model != ChartView.DRAW_URINEVOLUME) return;

        this.left = left;
        this.right = right;
        this.top = top;
        this.bottom = bottom;

        this.marginLeft = marginLeft;
        this.marginTop = marginTop;
        this.marginRight = marginRight;
        this.marginBottom = marginBottom;

        this.paddingLeft = paddingLeft;
        this.paddingTop = paddingTop;
        this.paddingRight = paddingRight;
        this.paddingBottom = paddingBottom;

        this.width = width;
        this.height = height;
        this.centerX = centerX;
        this.centerY = centerY;
    }

    @Override
    public void onDrawNull(Context context, ChartView chart, Canvas canvas, String hint, int model) {

        if (model != ChartView.DRAW_URINEVOLUME) return;

        canvas.save();
        drawBackground(context, chart, canvas, hint);
        canvas.restore();
    }

    @Override
    public void onDrawData(Context context, ChartView chart, Canvas canvas, float xHighligh, float yHighligh, int model) {

        if (model != ChartView.DRAW_URINEVOLUME) return;
        minValue = 0f; // Y轴最小值
        maxValue = 10000f; // Y轴最大值

        canvas.save();
        drawLine(context, chart, canvas, xHighligh, yHighligh);
        drawBackground(context, chart, canvas, null);
        canvas.restore();
    }

    private final void drawLine(Context context, ChartView chart, Canvas canvas, float xHighligh, float yHighligh) {

        if (chart.isEmpty()) return;

        final float chartLeft = left + marginLeft + paddingLeft + DimenUtil.dp2px(10);
        final float chartTop = top + marginTop + paddingTop;
        final float chartRight = right - marginRight - paddingRight;
        final float chartBottom = bottom - marginBottom - paddingBottom;
        final float chartHeight = chartBottom - chartTop;
        final float chartWidth = chartRight - chartLeft;

        float value1 = 0f, value2 = 0f, value3 = 0f;

        final float paddingOutside = DimenUtil.dp2px(2.4f);
        final float paddingInside = DimenUtil.dp2px(0.6f);

        final int dotCountDefault = chart.getDotCountDefault();
        final float segment = chartWidth / dotCountDefault;

        final List<? extends Entry> list = chart.getEntryList();

        final float temp = maxValue - minValue;
        final float centerX = chartWidth * 0.5f + chartLeft;
        final float minX = centerX - segment;
        final float maxX = centerX + segment;

        for (Entry entry : list) {

            final String name = entry.getName();
            if (TextUtils.isEmpty(name)) continue;

            final float value = entry.getValue();
            if (value == 0f) continue;

            if ("入量".equals(name)) {
                value1 += value;
            } else if ("白天尿量".equals(name)) {
                value2 += value;
            } else {
                value2 += value;
                value3 += value;
            }
        }

        final float y1 = (maxValue - (value1 > maxValue ? maxValue : value1)) * chartHeight / temp + chartTop;
        final float y2 = (maxValue - (value2 > maxValue ? maxValue : value2)) * chartHeight / temp + chartTop;
        final float y3 = (maxValue - (value3 > maxValue ? maxValue : value3)) * chartHeight / temp + chartTop;

        Paint rectPaint1 = StockPaint.getRectPaint(StockPaint.GREEN);
        canvas.drawRect(minX + paddingOutside, y1, centerX - paddingInside, chartBottom, rectPaint1);
        Paint rectPaint2 = StockPaint.getRectPaint(StockPaint.ORANGE);
        canvas.drawRect(centerX + paddingInside, y2, maxX - paddingOutside, y3, rectPaint2);
        Paint rectPaint3 = StockPaint.getRectPaint(StockPaint.BLUE);
        canvas.drawRect(centerX + paddingInside, y3, maxX - paddingOutside, chartBottom, rectPaint3);

        if(value1 != 0){
            final Paint axisPaint1 = StockPaint.getAxisTextPaint(Paint.Align.CENTER, StockPaint.BLACK);
            final String str1 = String.valueOf((int) value1);
            axisPaint1.getTextBounds(str1, 0, str1.length(), mTextRect);
            canvas.drawText(str1, minX + paddingOutside + segment * 0.5f, y1 + mTextRect.height(), axisPaint1);
        }

        if(value2 != 0){
            final Paint axisPaint2 = StockPaint.getAxisTextPaint(Paint.Align.CENTER, StockPaint.BLACK);
            final String str2 = String.valueOf((int) value2);
            axisPaint2.getTextBounds(str2, 0, str2.length(), mTextRect);
            canvas.drawText(str2, centerX + paddingInside + segment * 0.5f, y2 + mTextRect.height(), axisPaint2);
        }

        if(value3 != 0){
            final Paint axisPaint3 = StockPaint.getAxisTextPaint(Paint.Align.CENTER, StockPaint.BLACK);
            final String str3 = String.valueOf((int) value3);
            axisPaint3.getTextBounds(str3, 0, str3.length(), mTextRect);
            canvas.drawText(str3, centerX + paddingInside + segment * 0.5f, y3 + mTextRect.height(), axisPaint3);
        }

        final Paint axisPaint = StockPaint.getAxisTextPaint(Paint.Align.CENTER, StockPaint.BLACK);
        final String str4 = list.get(0).getDate();
        axisPaint.getTextBounds(str4, 0, str4.length(), mTextRect);
        canvas.drawText(str4, centerX, chartBottom + mTextRect.height() * 1.5f, axisPaint);
    }

    private final void drawBackground(Context context, ChartView chart, Canvas canvas, String hint) {

        final float chartLeft = left + marginLeft;
        final float chartTop = top + marginTop;
        final float chartRight = right - marginRight;
        final float chartBottom = bottom - marginBottom;
        final float chartHeight = chartBottom - chartTop;
        final float chartWidth = chartRight - chartLeft;

        // x轴
        canvas.drawLine(chartLeft, chartTop, chartLeft, chartBottom, StockPaint.getAxisPaint(StockPaint.BLACK));
//        final float segmentX = (chartWidth - paddingLeft - paddingRight) / (chart.getDotCountDefault() - 1);
//        final int begin = chart.getDotBegin();
//        final int end = Math.max(chart.getDotEnd(), chart.getDotCountDefault()) - 1;
//        for (int i = begin; i <= end; i++) {

//            final int position = (i - chart.getDotBegin());
//            final float textX = chartLeft + paddingLeft + segmentX * position;
//            if (i > begin) {
//                canvas.drawLine(textX, chartBottom - DimenUtil.dp2px(4), textX, chartBottom, StockPaint.getLinePaint(StockPaint.BLACK));
//            }

//            if (chart.isEmpty() || position >= chart.getDotCountReal() || i < 0) continue;
//
//            final Entry entry = chart.getEntryList().get(i);
//            final String date = entry.getDate();
//            if (i == begin) {
//                final Paint axisPaint = StockPaint.getAxisTextPaint(Paint.Align.LEFT, StockPaint.BLACK);
//                axisPaint.getTextBounds(date, 0, date.length(), mTextRect);
//                canvas.drawText(date, textX, chartBottom + mTextRect.height() * 1.5f, axisPaint);
//            } else if (i == end) {
//                final Paint axisPaint = StockPaint.getAxisTextPaint(Paint.Align.RIGHT, StockPaint.BLACK);
//                axisPaint.getTextBounds(date, 0, date.length(), mTextRect);
//                canvas.drawText(date, textX, chartBottom + mTextRect.height() * 1.5f, axisPaint);
//            } else {
//                final Paint axisPaint = StockPaint.getAxisTextPaint(Paint.Align.CENTER, StockPaint.BLACK);
//                axisPaint.getTextBounds(date, 0, date.length(), mTextRect);
//                canvas.drawText(date, textX, chartBottom + mTextRect.height() * 1.5f, axisPaint);
//            }
//        }

        // y轴
        canvas.drawLine(chartLeft, chartBottom, chartRight, chartBottom, StockPaint.getAxisPaint(StockPaint.BLACK));
        // 单位
        final String unit = "ml";
        final Paint unitPaint = StockPaint.getAxisTextPaint(Paint.Align.LEFT, StockPaint.BLACK, DimenUtil.sp2px(12.4f));
        unitPaint.getTextBounds(unit, 0, unit.length(), mTextRect);
        canvas.drawText(unit, chartLeft - mTextRect.width() * 0.5f, chartTop - mTextRect.height(), unitPaint);

        final float textX = chartLeft * 0.9f;

        for (int i = 0; i <= chart.getSegmentY(); i++) {

            final float textY = chartTop + i * (chartHeight / chart.getSegmentY());
            if (i < chart.getSegmentY()) {
                canvas.drawLine(chartLeft, textY, chartLeft + DimenUtil.dp2px(4), textY, StockPaint.getLinePaint(StockPaint.BLACK));
            }
            if (chart.isEmpty()) continue;

            final float average = (maxValue - minValue) / chart.getSegmentY();
            if (i == 0) {
                final String str = String.valueOf((int) maxValue);
                final Paint axisPaint = StockPaint.getAxisTextPaint(Paint.Align.RIGHT, StockPaint.BLACK);
                axisPaint.getTextBounds(str, 0, str.length(), mTextRect);
                canvas.drawText(str, textX, chartTop + mTextRect.height(), axisPaint);
            } else if (i == chart.getSegmentY()) {
                final String str = String.valueOf((int)minValue);
                final Paint axisPaint = StockPaint.getAxisTextPaint(Paint.Align.RIGHT, StockPaint.BLACK);
                canvas.drawText(str, textX, chartBottom, axisPaint);
            } else {
                final String str = String.valueOf((int)(maxValue - average * i));
                final Paint axisPaint = StockPaint.getAxisTextPaint(Paint.Align.RIGHT, StockPaint.BLACK);
                axisPaint.getTextBounds(str, 0, str.length(), mTextRect);
                canvas.drawText(str, textX, textY + mTextRect.height() * 0.5f, axisPaint);
            }
        }

        // 安全值
//        if (!chart.isEmpty()) {
//
//            // Max
//            final float warningMax1 = maxValue - 10f;
//            final float warningMax2 = maxValue - minValue;
//            final float warningMaxY = warningMax1 * chartHeight / warningMax2 + chartTop;
//            final float warningMaxBegin1 = chartLeft + 2;
//            final float warningMaxBegin2 = warningMaxBegin1 + 5;
//            mDashLine[0] = warningMaxBegin1;
//            mDashLine[2] = warningMaxBegin2;
//            mDashLine[1] = mDashLine[3] = warningMaxY;
//            while (mDashLine[0] <= chartRight) {
//                canvas.drawLines(mDashLine, StockPaint.getDashPaint(StockPaint.BLACK));
//                final float x1Temp = mDashLine[0];
//                final float x2Temp = mDashLine[2];
//                mDashLine[0] = x1Temp + 15;
//                mDashLine[2] = Math.min(x2Temp + 15, right);
//            }
////            final String warningMaxText = String.valueOf(37.3);
////            final Paint warningMaxTextPaint = StockPaint.getAxisTextPaint(Paint.Align.RIGHT, StockPaint.BLACK);
////            warningMaxTextPaint.getTextBounds(warningMaxText, 0, warningMaxText.length(), mTextRect);
////            canvas.drawText(warningMaxText, textX, warningMaxY + mTextRect.height() * 0.5f, warningMaxTextPaint);
//
//            // Min
//            final float warningMin1 = maxValue - 2.8f;
//            final float warningMin2 = maxValue - minValue;
//            final float warningMinY = warningMin1 * chartHeight / warningMin2 + chartTop;
//            final float warningMinBegin1 = chartLeft + 2;
//            final float warningMinBegin2 = warningMinBegin1 + 5;
//            mDashLine[0] = warningMinBegin1;
//            mDashLine[2] = warningMinBegin2;
//            mDashLine[1] = mDashLine[3] = warningMinY;
//            while (mDashLine[0] <= chartRight) {
//                canvas.drawLines(mDashLine, StockPaint.getDashPaint(StockPaint.BLACK));
//                final float x1Temp = mDashLine[0];
//                final float x2Temp = mDashLine[2];
//                mDashLine[0] = x1Temp + 15;
//                mDashLine[2] = Math.min(x2Temp + 15, right);
//            }
////            final String warningMinText = String.valueOf(35.7);
////            final Paint warningMinTextPaint = StockPaint.getAxisTextPaint(Paint.Align.RIGHT, StockPaint.BLACK);
////            warningMinTextPaint.getTextBounds(warningMinText, 0, warningMinText.length(), mTextRect);
////            canvas.drawText(warningMinText, textX, warningMinY + mTextRect.height() * 0.5f, warningMinTextPaint);
//        }

        if (TextUtils.isEmpty(hint)) return;
        final Paint hintPaint = StockPaint.getAxisTextPaint(Paint.Align.CENTER, StockPaint.BLACK, DimenUtil.sp2px(12.4f));
        canvas.drawText(hint, centerX, centerY, hintPaint);
    }
}